package com.lrl.controller;

import com.lrl.pojo.User;
import com.lrl.service.BookService;
import com.lrl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    UserService userService;
    BookService bookService;

    @Autowired
    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/login")
    public String login(User user, Model model, HttpSession session) {
        if (userService.loginChecked(user)) {
            model.addAttribute("booksList", bookService.queryAllBook());
            return "allBooks";
        } else {
            session.setAttribute("error", "账号或密码错误，请重新登录！");
            return "error";
        }
    }

    @RequestMapping("/check_username")
    @ResponseBody
    public String check_name(String username) {
        String msg = "";
        if (userService.checkName(username)) {
            msg = "ok";
        } else {
            msg = "用户名不存在";
        }
        return msg;
    }
}
